package miiteen.sys.shiro;

import miiteen.common.utils.Exceptions;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author huangzhanhu
 * @version 创建时间：2014年11月7日  上午9:44:15
 */
public class MtFormAuthenticationFilter extends FormAuthenticationFilter {

    private static final Logger log = LoggerFactory.getLogger(FormAuthenticationFilter.class);

    /**
     * 覆盖isAccessAllowed，改变shiro的验证逻辑。
     * 避免不能多次登录的错误。
     *
     * @param request
     * @param response
     * @param mappedValue
     * @return
     * @see org.apache.shiro.web.filter.authc.AuthenticatingFilter#isAccessAllowed(javax.servlet.ServletRequest,
     * javax.servlet.ServletResponse, java.lang.Object)
     */
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        try {
            // 先判断是否是登录操作
            if (isLoginSubmission(request, response)) {
                if (log.isTraceEnabled()) {
                    log.trace("Login submission detected.  Attempting to execute login.");
                }
                return false;
            }
        } catch (Exception e) {
            log.error(Exceptions.getStackTraceAsString(e));
        }

        return super.isAccessAllowed(request, response, mappedValue);
    }
}
